package com.bdzl.admin.service.sys.impl;

import com.bdzl.admin.service.sys.OrgService;
import com.bdzl.admin.service.sys.UserOrgService;
import com.bdzl.pub.entity.sys.UserOrgDO;
import com.bdzl.pub.vo.sys.OrgDeptVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;

import com.bdzl.admin.dao.sys.OrgDeptDao;
import com.bdzl.pub.entity.sys.OrgDeptDO;
import com.bdzl.admin.service.sys.OrgDeptService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class OrgDeptServiceImpl implements OrgDeptService {
	@Autowired
	private OrgDeptDao orgDeptDao;
	@Autowired
	private UserOrgService userOrgService;
	@Autowired
	private OrgService orgService;

	@Override
	public OrgDeptDO get(String deptNo){
		return orgDeptDao.get(deptNo);
	}
	
	@Override
	public List<OrgDeptVO> list(Map<String, Object> map){
		List<OrgDeptDO> list = orgDeptDao.list(map);
		List<OrgDeptVO> orgDeptVOList = new ArrayList<>();
		for (OrgDeptDO orgDeptDO:list) {
			OrgDeptVO orgDeptVO = new OrgDeptVO();
			//类拷贝
			BeanUtils.copyProperties(orgDeptDO, orgDeptVO);
			if (orgDeptDO.getDeptPno().equals("1")){
				orgDeptVO.setDeptPname(orgService.get("1").getOrgName());
			}else {
				orgDeptVO.setDeptPname(get(orgDeptDO.getDeptPno()).getDeptName());
			}
			orgDeptVOList.add(orgDeptVO);
		}
		return orgDeptVOList;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orgDeptDao.count(map);
	}
	
	@Override
	public int save(OrgDeptDO orgDept){
		//编号查重
		OrgDeptDO orgDeptDO = orgDeptDao.get(orgDept.getDeptNo());
		//名称查重
		Map<String,Object> map = new HashMap<>();
		map.put("deptName",orgDept.getDeptName());
		List<OrgDeptDO> list = orgDeptDao.list(map);
		if (list.size()>0){
			return -11;
		}
		if (orgDeptDO!=null){
			return -10;
		}
		orgDept.setDeptNo(orgDept.getDeptPno()+"_"+orgDept.getDeptNo());
		return orgDeptDao.save(orgDept);
	}
	
	@Override
	public int update(OrgDeptDO orgDept){
		return orgDeptDao.update(orgDept);
	}
	
	@Override
	@Transactional
	public int remove(String deptNo){
		int i = deleteJarEntry(deptNo);
		if (i>0){
			return i;
		}
		return orgDeptDao.remove(deptNo);
	}
	
	@Override
	@Transactional
	public int batchRemove(String[] deptNos){
		for (String item:deptNos) {
			int i = deleteJarEntry(item);
			if (i>0){
				return i;
			}
		}
		return orgDeptDao.batchRemove(deptNos);
	}

	/**
	 *  根据部门编号 查询是否有下级部门 或 员工
	 * @param deptNo
	 * @return
	 */
	public int deleteJarEntry(String deptNo){
		Map<String,Object> map = new HashMap();
		map.put("suoDeptNo",deptNo);
		//查询旗下是否有用户
		List<UserOrgDO> userOrgDOList = userOrgService.list(map);
		map.put("deptPno",deptNo);
		//查询旗下是否有部门
		List<OrgDeptDO> orgDeptDOList = orgDeptDao.list(map);
		if (userOrgDOList.size()>0){
			return 99;//有用户
		}
		if (orgDeptDOList.size()>0){
			return 100;//有部门
		}
		return 0;
	}
}
